package com.dede.rxbase.dialog;


import android.os.Build;
import android.os.Bundle;
import android.support.annotation.IntDef;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.dede.rxbase.R;
import com.dede.rxbase.activity.BaseActivity;
import com.dede.rxbase.fragment.BaseDialogFragment;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

/**
 * 不同状态的Dialog，推荐使用Builder模式创建，同时也可以作为普通弹窗使用
 * Created by dede on 2018/2/27.
 */
public class StateDialog extends BaseDialogFragment implements View.OnClickListener {

    public final static int ERROR = -1;
    public final static int EMPTY = 0;
    public final static int SUCCESS = 1;
    public final static int LOADING = 2;

    @IntDef({ERROR, EMPTY, LOADING, SUCCESS})
    @Retention(RetentionPolicy.SOURCE)
    public @interface State {
    }

    private boolean fullContent;
    @LayoutRes
    private int layout;
    private boolean postEvent;
    private boolean transparentBg;
    @Nullable
    private View.OnClickListener reloadListener;

    public void setOnReloadListener(View.OnClickListener reloadListener) {
        this.reloadListener = reloadListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        final Window window = getDialog().getWindow();

        window.setWindowAnimations(R.style.DialogFadeAnim);

        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);// 去除标题，主要针对4.4及以下版本的Dialog的默认title
        window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);// 去除半透明背景

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            window.getDecorView().setElevation(0);// 去除海拔阴影
        }

        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return this.layout;
    }

    @Override
    protected void initDialog() {
        if (fullContent) {
            fullContent();
        }
    }

    @Override
    protected void initView() {
        Window window = getDialog().getWindow();
        if (transparentBg) {
            window.setBackgroundDrawableResource(android.R.color.transparent);
        } else {
            window.setBackgroundDrawableResource(android.R.color.background_light);
        }

        if (postEvent) {
            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);// 向下传递触摸事件
            window.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);// 向下传递按键事件
        }

        if (reloadListener != null) {
            getView().setOnClickListener(this);
        }
    }

    @Override
    public void onClick(View v) {
        reloadListener.onClick(v);
        dismiss();
    }

    /**
     * 使DialogFragment填充界面
     */
    public void fullContent() {
        final Window window = getDialog().getWindow();
        FragmentActivity activity = getActivity();
        int contentHeight;// 页面减去标题栏的高度
        if (activity instanceof BaseActivity) {
            contentHeight = ((BaseActivity) activity).getContentView().getHeight();
        } else {
            contentHeight = activity.getWindow().getDecorView().findViewById(android.R.id.content).getHeight();
        }

        window.setGravity(Gravity.BOTTOM);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, contentHeight);// 必须放在onResume
    }

    /**
     * 使DialogFragment自适应
     */
    public void wrapContent() {
        final Window window = getDialog().getWindow();
        window.setGravity(Gravity.CENTER);
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);// 必须放在onResume
    }


    public static class Builder {

        private boolean isCancelable = true;
        private boolean fullContent;
        @LayoutRes
        private int layout;
        private boolean postEvent;// 向下层Window传递事件

        private boolean transparentBg;
        @Nullable
        private View.OnClickListener reloadListener;

        /**
         * Dialog Layout
         *
         * @param layout
         * @return
         */
        public Builder layout(@LayoutRes int layout) {
            this.layout = layout;
            return this;
        }

        public Builder setCancelable(boolean isCancelable) {
            this.isCancelable = isCancelable;
            return this;
        }

        /**
         * Dialog填充
         *
         * @return
         */
        public Builder fullContent() {
            this.fullContent = true;
            return this;
        }

        /**
         * 向下层window传递事件
         *
         * @param postEvent true 传递；false 不传递
         * @return
         */
        public Builder postEvent(boolean postEvent) {
            this.postEvent = postEvent;
            return this;
        }

        /**
         * 透明背景，默认白色
         *
         * @return
         */
        public Builder transparentBg() {
            transparentBg = true;
            return this;
        }

        /**
         * 重新加载回调
         *
         * @param listener
         * @return
         */
        public Builder reloadListener(View.OnClickListener listener) {
            this.reloadListener = listener;
            return this;
        }

        public StateDialog builder() {
            StateDialog dialog = new StateDialog();
            dialog.layout = layout;
            dialog.fullContent = fullContent;
            dialog.reloadListener = reloadListener;
            dialog.postEvent = postEvent;
            dialog.transparentBg = transparentBg;
            dialog.setCancelable(isCancelable);
            return dialog;
        }

        public void show(FragmentActivity activity) {
            builder().show(activity);
        }

        public void show(Fragment fragment) {
            builder().show(fragment);
        }

    }
}
